import { WrapIssueForTable } from "gitee-simple-db";
import _ from "lodash";
import store from "../store/store";
const Operation = new WrapIssueForTable("user", {transBodyToJSON: true})

export interface IUser {
    nickName: string,
    bindingId: string,
    inviteCode: string,
    gender: "male" | "female",
    score: string,
    id?: string
}
const getAllUser = async():Promise<IUser[]> => {
    let allUser = await Operation.getAllData()
    if (_.isEmpty(allUser)) {
        throw Error("user list is empty.")
    }
    store.userList = allUser
    return allUser;
}

const User = {
    operation: Operation,
    getAllUser,
}
export default User;
// import { globalData } from "./data"
// const gitdb = (<any>window).gitdb;

// const tableName = 'user'

// const getTableId = (table = tableName) => {
//     return globalData.tableMap ? globalData.tableMap[table] : 'XXXXX';
// }
// const transCommentToData = (comment: any) => {
//     try {
//         let commentInfo = JSON.parse(comment.body)
//         return {...commentInfo, id: comment.id}
//     } catch (e) {
//         console.error("transform comment to data error", comment, "error:", e)
//     }
// }
// 
// const updateUser = async (data: any) => {
//     let updateResult = await gitdb.updateOneIssueComment(data.id, JSON.stringify(data, null, 4))
//     return transCommentToData(updateResult);
// }
// const updateUserKeysById = async(userId: number, data: any) => {
//     let userData = await getOneUser(userId)
//     Object.keys(data).forEach(key => {
//         if (userData[key] !== undefined) {
//             userData[key] = data[key]
//         } else {
//             console.warn(`user ${userId} has no attribute: ${key}, ignore it.`)
//         }
//     })
//     return await updateUser(userData)
// }
// const bindUser = async(inviteCode: string, currentUserid: number) => {
//     let correctUser = globalData.userList.filter(user => user.inviteCode === inviteCode)
//     if (correctUser.length > 0) {
//         if (correctUser.length > 1) {
//             console.error(`There are more than one user has the same invite code: ${inviteCode}, here bind the first user:${JSON.stringify(correctUser[0])}`)
//         }
//         let inviteUser = correctUser[0];
//         if (inviteUser.bindingId) {
//             if (inviteUser.bindingId === currentUserid) {
//                 return 'has binded!'
//             } else {
//                 throw Error('has binded other user!')
//             }
//         }
        
//         inviteUser.bindingId = currentUserid;
//         updateUser(inviteUser)
//         updateUserKeysById(currentUserid, {bindingId: inviteUser.id})
//     } else {
//         throw Error(`incorrect invite code:${inviteCode}`)
//     }
//     return 'bind success!'
// }
// const getOneUser = async (userid:number) => {
//     let userResp = await gitdb.getOneComment(userid)
//     return transCommentToData(userResp)
// }
// const addScore = async(userid: number, score: number) => {
//     if (score === 0 || !score) {
//         return 
//     }
//     let user = await getOneUser(userid)
//     user.score = (parseFloat(user.score) + score).toFixed(3)
//     return await updateUser(user);
// }
// const login = (name:string) => {
//     if (globalData.login) {
//         console.log('has logined!')
//         return globalData.login
//     }
//     let user;
//     // console.log(`${!globalData.userList}--${!globalData.userList.length}`)
//     if (globalData.userList && globalData.userList.length) {
//         user = globalData.userList.filter(user => user.nickName === name)
//         if (!user || !user.length) {
//             window.localStorage.removeItem("$NICK_NAME")
//             window.localStorage.removeItem("$ACCESS_TOKEN")
//             throw Error('未注册的用户!')
//         }
//         if (user.length > 1) {
//             console.error("多个用户名相同!", user)
//         }
//     } else {
//         throw Error('请刷新浏览器重新加载数据后再尝试登录')
//     }
//     globalData.login = user[0]
//     return user[0];
// }

// export const UserOperation = {
//     getAllUser,
//     bindUser,
//     addScore,
//     getOneUser,
//     login
// }